package com.bjx.api.system.util;

import cn.hutool.core.collection.CollectionUtil;
import com.bjx.api.system.domain.vo.DictDataVO;
import com.bjx.api.system.service.RemoteDictService;
import com.bjx.common.core.constant.FrameConst;
import com.bjx.common.core.domain.R;
import com.bjx.common.core.exception.BJXException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @desc: 字典工具
 * @author: liushuai
 * @date: 2022/9/22 17:03
 */
@Component("dictService")
public class DictService {
    @Autowired
    private RemoteDictService remoteDictService;

    public List<DictDataVO> listDictData(String dictTypeCode){
        R<List<DictDataVO>> res = remoteDictService.listCacheDictDataByDictTypeCode(dictTypeCode);
        if(res.getCode() == FrameConst.FAIL.intValue()){
            throw new BJXException("字典调用失败");
        }
        return res.getData();
    }

    /**
     * 根据字典值获取字典描述
     * @param dictDataList
     * @param value
     * @return: java.lang.String
     * @Author: liushuai
     * @Date: 2022/9/22 17:11
     */
    public static String getDictLabel(List<DictDataVO> dictDataList, String value) {
        if (CollectionUtil.isEmpty(dictDataList) || value == null) {
            return "";
        }
        return dictDataList.stream().filter(res -> value.equals(res.getValue())).map(DictDataVO::getLabel).findFirst().orElse("");
    }
}
